package com.fept.msgcore.msgserver.protocol.model.option;

import com.fept.msgcore.msgserver.protocol.model.base.ProtocolConstants;

/**
 * Created by Srma on 2/9/2017.
 */
public class SignOption extends BaseOption {

    private static final long serialVersionUID = -7419903422859803545L;

    public static final int NONE = 0x00;
    public static final int MD5 = 0x01;
    public static final int SHA1 = 0x02;
    public static final int HMAC_MD5 = 0x03;
    public static final int HMAC_SHA1 = 0x04;

    private int algorithm;
    private int key;
    private byte[] mac;

    public SignOption() {
        this.setType(ProtocolConstants.OPTION_SIGN);
    }

    /**
     *
     */
    @Override
    public byte[] pack() {
        byte[] optionBytes = new byte[1 + 1 + this.mac.length];
        optionBytes[0] = (byte) (this.algorithm & 0xFF);
        optionBytes[1] = (byte) (this.key & 0xFF);
        System.arraycopy(this.mac, 0, optionBytes, 2, this.mac.length);
        this.setValue(optionBytes);
        this.setLength(optionBytes.length);
        return super.pack();
    }

    /**
     *
     */
    @Override
    public void unpack(byte[] data, int offset) {
        super.unpack(data, offset);

        data = this.getValue();
        offset = 0;

        this.setAlgorithm(data[offset]);
        offset += 1;

        this.setKey(data[offset]);
        offset += 1;

        byte[] mac = new byte[this.getLength() - 2];
        System.arraycopy(data, offset, mac, 0, mac.length);
        this.setMac(mac);
    }

    /**
     * @return the algorithm
     */
    public int getAlgorithm() {
        return algorithm;
    }

    /**
     * @param algorithm
     *            the algorithm to set
     */
    public void setAlgorithm(int algorithm) {
        this.algorithm = algorithm;
    }

    /**
     * @return the key
     */
    public int getKey() {
        return key;
    }

    /**
     * @param key
     *            the key to set
     */
    public void setKey(int key) {
        this.key = key;
    }

    /**
     * @return the mac
     */
    public byte[] getMac() {
        return mac;
    }

    /**
     * @param mac
     *            the mac to set
     */
    public void setMac(byte[] mac) {
        this.mac = mac;
    }
}
